package org.esisar.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.esisar.model.Model;
import org.esisar.model.Picture;

public class PictureStatementCreator implements StatementCreator{


	private PreparedStatement preparedStatement;
	long x;
	
	@Override
	public PreparedStatement addStatement(Connection connection, Model model) throws SQLException {
		
		Picture picture = modelToPicture(model);
		
		String[] key = new String[]{"idPicture"};
		preparedStatement = connection.prepareStatement("INSERT INTO picture (idPicture,url,camera,lens,focal,sensitivity,opening,resolution,obturation,idClient) VALUES (picture_sequence.nextval,?,?,?,?,?,?,?,'"
				+ picture.getObturation() +"','" + picture.getIdClient() + "')",key);

		fillPicture(picture);
	
		return preparedStatement;
		
	}

	@Override
	public PreparedStatement deleteStatement(Connection connection, List<Long> keys) throws SQLException {

		preparedStatement = connection.prepareStatement("DELETE FROM picture WHERE idPicture=?");

		preparedStatement.setLong(1, keys.get(0));
				
		return preparedStatement;
	}

	@Override
	public PreparedStatement updateStatement(Connection connection, Model model)
			throws SQLException {
		
		Picture picture = modelToPicture(model);
		
		preparedStatement = connection.prepareStatement("UPDATE picture SET url = ?,camera = ?,lens = ?,focal = ?,sensitivity = ?,opening = ?,resolution = ?,obturation = ?,idClient = ? WHERE idPicture = ?");

		fillPicture(picture);
		preparedStatement.setLong(10, picture.getIdPicture());
		
		return preparedStatement;
	}

	private void fillPicture(Picture picture) throws SQLException {
		preparedStatement.setString(1, picture.getUrl());
		preparedStatement.setString(2, picture.getCamera());
		preparedStatement.setString(3, picture.getLens());
		preparedStatement.setString(4, picture.getFocal());
		preparedStatement.setString(5, picture.getSensitivity());
		preparedStatement.setString(6, picture.getOpening());
		preparedStatement.setString(7, picture.getResolution());
	//	preparedStatement.setString(8, picture.getObturation());
	//	preparedStatement.setString(8, picture.getIdClient()+"");	//TODO - bad mapping to string
	}

	private Picture modelToPicture(Model model) {
		Picture picture;
		
		if (model instanceof Picture){
			picture = (Picture) model;
		}else{
			throw new ClassCastException("Bad object instance");
		}
		return picture;
	}
	
	@Override
	public PreparedStatement readStatement(Connection connection, String field,String value) throws SQLException {

		preparedStatement = connection.prepareStatement("select * from picture where " + field + " = ? ");

		preparedStatement.setString(1, value);
		
		return preparedStatement;

	}

	@Override
	public Model fillModel(ResultSet rs) throws SQLException {
		String idPicture = rs.getString("idPicture");
		String url = rs.getString("url");
		String camera = rs.getString("camera");
		String lens = rs.getString("lens"); 
		String focal = rs.getString("focal");
		String sensitivity = rs.getString("sensitivity");
		String opening = rs.getString("opening");
		String resolution = rs.getString("resolution");
		String obturation = rs.getString("obturation");
		String idClient = rs.getString("idClient");
	
		Picture picture = new Picture(url, camera, lens, focal, sensitivity, opening, resolution, obturation, Long.parseLong(idClient));
		picture.setIdPicture(Long.parseLong(idPicture));
		
		return picture;
	}

	public PreparedStatement getPreparedStatement() {
		return preparedStatement;
	}

	public void setPreparedStatement(PreparedStatement preparedStatement) {
		this.preparedStatement = preparedStatement;
	}
}